package project;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import java.util.Arrays;
import java.util.Properties;


public class KafkaConsumer {
    public static void main(String[] args) {

        // 指定kafka消费者的配置信息
        Properties properties = new Properties();
        // 连接kafka
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.57.11:9092");
        // 反序列化
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringDeserializer");
        // 配置消费者group id，自定义 id 名字
        properties.put(ConsumerConfig.GROUP_ID_CONFIG,"niit01");
        // 数据从最开始读取
        properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");
        // 创建消费者
        org.apache.kafka.clients.consumer.KafkaConsumer<String,String> consumer = new org.apache.kafka.clients.consumer.KafkaConsumer<String, String>(properties);
        // 订阅某个topic
        consumer.subscribe(Arrays.asList("stuInfo"));

        while(true){
            // topic stuInfo 读取 topic数据（拉取） 阻塞超时时间
            ConsumerRecords<String,String> consumerRecords = consumer.poll(5);

            for (ConsumerRecord<String,String> record:consumerRecords){
                //System.out.println("key: " + record.key());
                System.out.println( record.value());
            }
        }



    }
}
